window.LS.problem = {
    init: function(list, description, buttons) {
        this.list = $(list);
        this.description = $(description);
        this.buttons = $(buttons);
        this.tmpdom = null;
        this.selected = null; // selected problem id
        this.selected_ans = 0;
        this.answer_commited = 0;
        this.alertbox = new LS.notifier('#problems_error')
    },
    onLogout: function() {
        this.list.children("div.problem").remove().each(function(){delete this;});
    },
    /* Problem Lists */
    reload: function() {
        var that = this;
        
        $.ajax({
            url: LS.getURL("problems"),
            type: "GET",
            dataType: "json",
            error: function(xhr) {
                LS.problem.alertbox.showError(LS.localized.problemListErrorConnection);
            },
            success: function(response) {
                LS.ajaxReturnedTimestamp(response);
                LS.problem.alertbox.showError(null);
                
                if(response.problem.success) {
                    that.update(response.problem.problems);
                } else {
                    LS.problem.alertbox.showError((response.problem.errmsg) ? response.problem.errmsg : LS.localized.temporaryError);
                }
            }
        });
    },
    update: function(problems) {
        for(var i in problems) {
            var item = problems[i];
            
            var elem = $('#problem_'+item.id);
            if(!elem.length) elem = this.newProblemRow(item);
            //else elem.children('a').html(item.title);
        }
        if(this.tmpdom != null) {
            this.list.append(this.tmpdom);
            this.tmpdom = null;
        }
        
        if(this.selected != null && $('#problem_'+this.selected).length) {
            //this.changeTo(this.selected);
        } else {
            if(problems.length > 0) this.changeTo(problems.pop().id);
        }
    },
    newProblemRow: function(p) {
        if(!this.tmpdom) this.tmpdom = document.createDocumentFragment();
        
        var elem = document.createElement("div");
        $(elem).attr('id', 'problem_'+p.id)
               .attr('problemid', p.id)
               .addClass('problem')
               .html(this.getProblemRowHTML(p))
               .fadeIn();
        
        this.tmpdom.appendChild(elem);
        
        return $(elem);
    },
    /* Problem Description */
    changeTo: function(id) {
        var that = this;
        
        $.ajax({
            url: LS.getURL("problems/get/"+id),
            type: "GET",
            dataType: "json",
            error: function(xhr) {
                LS.alertbox.showError(LS.localized.errorConnection);
            },
            success: function(response) {
                LS.ajaxReturnedTimestamp(response);
                LS.alertbox.showError(null);
                
                if(response.problem.success) {
                    that.showProblem(response.problem.problems[id]);
                } else {
                    LS.alertbox.showError((response.problem.errmsg) ? response.problem.errmsg : LS.localized.temporaryError);
                }
            }
        });
    },
    showProblem: function(p) {
        if(!p) {
            return;
        }
        if(this.selected == null || this.selected != p.id) {
            this.answer(null);
            
            this.selected = p.id;
            $('.select', this.list).removeClass('select');
        }
        $('#problem_'+p.id, this.list).addClass('select');
        this.description.html(this.getProblemDescriptionHTML(p));
        
        for(var i = 1; i <= 5; i++) {
            if(p.options[i-1] != "") {
                $('#btn_opt'+i+' a').html(this.getOptionHTML(i, p.options[i-1])).show();
            } else {
                $('#btn_opt'+i).hide();
            }
        }
        
        this.showProblemMsg(LS.localized.problemWaitForAnswer);
    },
    /* Answering */
    answer: function(sel) {
        if(sel != null && this.selected_ans == sel) {
            this.answerCommit();
            return;
        }
        
        this.selected_ans = 0;
        $('#buttons .ans_selected').removeClass('ans_selected');
        this.showProblemMsg(null);
        if(sel == null) {
            this.answer_commited = 0;
            return;
        }
        
        this.selected_ans = sel;
        
        $('#buttons #btn_opt'+sel).addClass('ans_selected');
        
        this.showProblemMsg(LS.localized.problemWaitForCommitment);
    },
    answerCommit: function() {
        if(this.selected == null) {
            this.showProblemMsg(LS.localized.problemNoProblemSelected);
        }
        if(this.selected_ans == 0) {
            this.showProblemMsg(LS.localized.problemNoAnswerSelected);
            return;
        }
        if(this.answer_commited == 1) {
            this.showProblemMsg(LS.localized.problemAnswerCommited);
            return;
        }
        
        this.answer_commited = 1;
        this.showProblemMsg(LS.localized.commitingAnswer);
        
        var that = this;
        
        $.ajax({
            url: LS.getURL("problems/answer/"+this.selected+"/"+this.selected_ans),
            type: "GET",
            dataType: "json",
            error: function(xhr) {
                that.showProblemMsg(LS.localized.errorConnection);
            },
            success: function(response) {
                if(response.problem.success) {
                    if(response.problem.correct == 1) {
                        that.showProblemMsg(LS.localized.answerCorrect);
                    } else {
                        that.showProblemMsg(LS.localized.answerWrong);
                    }
                } else {
                    that.showProblemMsg((response.problem.errmsg) ? response.problem.errmsg : LS.localized.temporaryError);
                }
            }
        });
    },
    showProblemMsg: function(msg) {
        if(msg == null) {
            $('#problem_msg').css({visibility: "hidden"});
            return;
        }
        $('#problem_msg').html(msg).css({visibility: "visible"});
    },
    /* HTML styles */
    getProblemRowHTML: function(p) {
        return "<p class='list_id'>"+p.id+".</p>&nbsp;&nbsp;<a href='' onclick='javascript:window.LS.problem.changeTo("+p.id+");return false;'><span class='problem_name'>"+p.title+"</span></a>";
        //return "ID:"+p.id+"<br>TITLE:"+p.title+"<br>CONTENT:"+p.content+"<br>OPTIONS:"+p.options.join("|")+"<br>";
    },
    getProblemDescriptionHTML: function(p) {
        return "<h1>"+p.title+"</h1><p>"+p.content+"</p>";
    },
    getOptionHTML: function(i, txt) {
        return "<p class='list_id'>&nbsp;"+i+".&nbsp;</p>&nbsp;&nbsp;"+txt;
    }
};
